from flask import Blueprint, request, render_template, g, redirect, url_for, jsonify, session

from apps.article.models import Article, Article_type, Comment
from apps.user.models import User
from extend import db

article_bp = Blueprint('article', __name__, url_prefix='/article')


# 自定义一个过滤器，用来在index首页转换二进制数据
@article_bp.app_template_filter('cdecode')
def content_decode(content):
    content = content.decode('utf8')
    return content

# 发表文章
@article_bp.route('/publish', methods=['GET', 'POST'])
def publish_article():
    if request.method == "POST":
        title = request.form.get('title')
        content = request.form.get('content')
        type_id = request.form.get('type')
        # 添加文章
        article = Article()
        article.title = title
        article.content = content
        article.type_id = type_id
        article.user_id = g.user.id
        db.session.add(article)
        db.session.commit()

        return redirect(url_for('user.index'))


# 文章详情
@article_bp.route('/detail')
def article_detail():
    # 获取文章对象通过id
    article_id = request.args.get('aid')
    article = Article.query.get(article_id)

    # 点击量变化
    article.click_num += 1
    db.session.commit()

    # 单独查询评论
    page = int(request.args.get('page', 1))
    comments = Comment.query.filter(Comment.article_id == article_id) \
        .order_by(-Comment.cdatetime) \
        .paginate(page=page, per_page=5)

    return render_template('article/detail.html', article=article,  comments=comments)


# 点赞
@article_bp.route('/love')
def article_love():
    article_id = request.args.get('aid')
    tag = request.args.get('tag')
    article = Article.query.get(article_id)
    # print('tag*************************',tag)
    # 判断是否已被点击过
    if tag == '1':
        article.love_num -= 1
    else:
        article.love_num += 1
    db.session.commit()

    return jsonify(num=article.love_num)


# 收藏
@article_bp.route('/save')
def article_save():
    article_id = request.args.get('aid')
    article = Article.query.get(article_id)
    tag = request.args.get('tag')
    # 判断是否已被点击过
    if tag == '1':
        article.save_num -= 1
    else:
        article.save_num += 1
    db.session.commit()

    return jsonify(num=article.save_num)


# 发表评论
@article_bp.route('/add_comment', methods=['GET', 'POST'])
def add_comment():
    if request.method == 'POST':
        comment_content = request.form.get('comment')
        article_id = request.form.get('aid')
        user_id = g.user.id
        # 评论模型
        comment = Comment()
        comment.user_id = user_id
        comment.comment = comment_content
        comment.article_id = article_id
        db.session.add(comment)
        db.session.commit()

        return redirect(url_for('article.article_detail') + '?aid=' + article_id)

    return redirect(url_for('user.index'))

# 文章分类检索
@article_bp.route('/type_search')
def type_search():
    # 用户对象的判断
    uid = session.get('uid')
    user = None
    if uid:
        user=User.query.get(uid)

    # 文章分类获取
    article_types=Article_type.query.all()

    # tid的获取
    tid = int(request.args.get('tid',1))
    # type = Article_type.query.get(tid)
    page = int(request.args.get('page', 1))

    # 分页器？？？？
    # pagination对象
    articles = Article.query.filter(Article.type_id == tid).paginate(page=page, per_page=3)

    return render_template('article/article_type.html',user=user,article_types=article_types,articles=articles )

